package com.srb.core.controller.api;


import com.srb.base.util.JwtUtils;
import com.srb.common.result.R;
import com.srb.core.pojo.vo.BorrowerVo;
import com.srb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author dapeng
 * @since 2021-08-08
 */
@Api(tags = {"借款人"})
@RestController
@RequestMapping("/api/core/borrower")
public class ApiBorrowerController {

    @Autowired
    private BorrowerService borrowerService;

    @ApiOperation(value = "保存借款人信息")
    @PostMapping(value = {"/auth/save"})
    public R save(@RequestBody BorrowerVo borrowerVo, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        borrowerService.saveBorrowerVoByUserId(borrowerVo,userId);
        return R.ok().message("信息提交成功");
    }

    @ApiOperation(value = "获取借款人认证状态")
    @GetMapping(value = {"/auth/getBorrowerStatus"})
    public R getBorrowerStatus(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        Integer status = borrowerService.getStatusByUserId(userId);
        return R.ok().data("borrowerStatus",status);
    }
}

